package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.PermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> pageResult = permissionService.query(qo);

        model.addAttribute("pageResult", pageResult);

        return "permission/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null){
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model) {
        if (id != null){
            Permission permission = permissionService.selectById(id);
            model.addAttribute("permission",permission);
        }

        return "permission/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {

        if (permission.getId() != null){
            permissionService.update(permission);
        }else {
            permissionService.save(permission);
        }
        return "redirect:/permission/list.do";
    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
    }
}
